﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using CsharpTpl.model;
using Lephone.Data;

namespace CsharpTpl.window
{
    public partial class FrmMain : Form
    {
        private FrmMainMenuHandler menuClickHandler;
        private List<long> czygnids = new List<long>();
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            DataSet ds = DbEntry.Context.ExecuteDataset("select distinct t_jsgn.gnid from t_jsgn ,t_czyjs where t_czyjs.czyid = " + FrmLogin.CZYID + " and t_czyjs.jsid= t_jsgn.jsid");
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                czygnids.Add(long.Parse(dt.Rows[i][0].ToString()));
            }
            menuClickHandler = new FrmMainMenuHandler(this);
            LoadMenu(0, null);
            MainMenu.Click += new EventHandler(menuClickHandler.HandleMenuClick);
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void LoadMenu(long fid,ToolStripMenuItem parent)
        {
            List<T_GN> topMenu = T_GN.Find(CK.K["FID"]==fid,new OrderBy("SXH"));
            if (topMenu.Count > 0)
            {
                List<ToolStripMenuItem> menuItems = new List<ToolStripMenuItem>();
                ToolStripMenuItem item;
                foreach (T_GN gn in topMenu)
                {
                    if (!czygnids.Contains(gn.Id))
                        continue;
                    if (gn.LX == 1)
                    {
                        item = new ToolStripMenuItem();
                        item.Name = gn.CZM;
                        item.Text = gn.MC;
                        item.Click += new EventHandler(menuClickHandler.HandleMenuClick);
                        addMenu(parent, item);
                        LoadMenu(gn.Id, item);
                    }
                    else
                    {
                        addMenu(parent, new ToolStripSeparator());
                    }
                    
                    
                }
                
                
            }
        }
        private void addMenu(ToolStripMenuItem parent,ToolStripItem child)
        {
            if (parent == null)
            {
                MainMenu.Items.Add(child);
            }
            else
            {
                parent.DropDownItems.Add(child);
            }
        }
    }
}
